package cn.shop.merchant.controller.gametemplate;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import cn.shop.merchant.model.user.MerchantAlias;
import cn.shop.merchant.req.AliasQueryReq;
import cn.shop.merchant.service.GameInfoService;
import cn.shop.merchant.service.MerchantAliasService;
import cn.shop.utils.ApiResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static cn.shop.utils.constant.Constant.UID;

/**
 * 二级域名相关接口
 *
 * @author 李一涵
 * @since 2022-05-01 10:18:00
 */
@RestController
@RequestMapping("/alias")
public class AliasController {

    @Resource
    private MerchantAliasService aliasService;
    @Resource
    private GameInfoService gameInfoService;

    private final static List<String> ALIAS_NAME = Arrays.asList("www", "vip", "gateway", "static");

    /**
     * 获取商户下二级域名列表
     *
     * @param req 查询条件
     * @return 返回结果
     */
    @GetMapping("/list")
    public ApiResult<Map<String, Object>> list(AliasQueryReq req) {
        long uid = StpUtil.getSession().getLong(UID);
        Map<String, Object> data = aliasService.aliasList(req, uid);
        return ApiResult.success(data);
    }

    /**
     * 获取所有域名
     *
     * @return 返回结果
     */
    @GetMapping("/all")
    public ApiResult<List<Map<String, Object>>> all() {
        long uid = StpUtil.getSession().getLong(UID);
        List<Map<String, Object>> data = aliasService.allAlias(uid);
        return ApiResult.success(data);
    }

    /**
     * 获取域名下所有游戏
     *
     * @param id 域名编号
     * @return 返回结果
     */
    @GetMapping("/game/{id}")
    public ApiResult<List<Map<String, Object>>> allGames(@PathVariable long id) {
        long uid = StpUtil.getSession().getLong(UID);
        List<Map<String, Object>> data = gameInfoService.allGame(uid, id);
        return ApiResult.success(data);
    }

    /**
     * 新增二级域名
     *
     * @param alias 二级域名
     * @return 返回结果
     */
    @PostMapping("")
    public ApiResult<Map<String, Object>> insert(@RequestBody @Validated MerchantAlias alias) {
        if (aliasService.hasAlias(alias.getAlias())) {
            return ApiResult.fail("二级域名已被注册");
        }
        for (String name : ALIAS_NAME) {
            if (name.equals(alias.getAlias())) {
                return ApiResult.fail("该域名属于预留域名，不能注册");
            }
        }
        long uid = StpUtil.getSession().getLong(UID);
        alias.setUid(uid);
        alias.setState(0);
        return ApiResult.result(aliasService.save(alias));
    }

    /**
     * 更新二级域名
     *
     * @param alias 二级域名
     * @return 返回结果
     */
    @PutMapping("")
    public ApiResult<Map<String, Object>> update(@RequestBody @Validated MerchantAlias alias) {
        long uid = StpUtil.getSession().getLong(UID);
        if (aliasService.hasAlias(alias.getAlias())) {
            return ApiResult.fail("二级域名已被注册");
        }
        for (String name : ALIAS_NAME) {
            if (name.equals(alias.getAlias())) {
                return ApiResult.fail("该域名属于预留域名，不能注册");
            }
        }
        return ApiResult.result(aliasService.updateAlias(alias, uid));
    }

    /**
     * 获取二级域名信息
     *
     * @param id 二级域名编号
     * @return 返回结果
     */
    @GetMapping("/{id}")
    public ApiResult<Map<String, Object>> getInfo(@PathVariable long id) {
        long uid = StpUtil.getSession().getLong(UID);
        return ApiResult.success(aliasService.aliasInfo(id, uid));
    }

    /**
     * 删除二级域名
     *
     * @param id 二级域名编号
     * @return 返回结果
     */
    @DeleteMapping("/{id}")
    public ApiResult<Map<String, Object>> delete(@PathVariable long id) {
        long uid = StpUtil.getSession().getLong(UID);
        long count = gameInfoService.countByAliasId(id);
        if (count > 0) {
            return ApiResult.fail("该域名下还有为移除的游戏，请先移除该域名下的游戏");
        }
        return ApiResult.result(aliasService.deleteAlias(id, uid));
    }

}
